# 12-1
import sys
import pygame

from pr_bmp import TestBmp

class Practice:
    def __init__(self):
        pygame.init()

        self.screen = pygame.display.set_mode((1200, 800))
        pygame.display.set_caption("12-1 Blue Window")

        self.testbmp = TestBmp(self)

    def run(self):
        while True:
            for event in pygame.event.get(): # event.get()返回一个列表，包含从上次该函数被调用后直到此次被调用所发生的所有事件
                self._check_event(event)

            self._update_screen()

# 12-2
    def _update_screen(self):
            self.screen.fill((0, 0, 255))
            self.testbmp.blitme()
            pygame.display.flip()

# 12-4
    def _check_event(self, event):
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            self._check_keydown_event(event)
        elif event.type == pygame.KEYUP:
            self._check_keyup_event(event)

    def _check_keydown_event(self, event):
        print(event.type)
        print(event.key)
        if event.key == pygame.K_q:
            sys.exit()
        elif event.key == pygame.K_LEFT:
            self.testbmp.moving_left = True
        elif event.key == pygame.K_RIGHT:
            self.testbmp.moving_right = True
        elif event.key == pygame.K_UP:
            self.testbmp.moving_up = True
        elif event.key == pygame.K_DOWN:
            self.testbmp.moving_down = True

    def _check_keyup_event(self, event):
        print(event.type)
        print(event.key)
        if event.key == pygame.K_LEFT:
            self.testbmp.moving_left = False
        elif event.key == pygame.K_RIGHT:
            self.testbmp.moving_right = False
        elif event.key == pygame.K_UP:
            self.testbmp.moving_up = False
        elif event.key == pygame.K_DOWN:
            self.testbmp.moving_down = False


if __name__ == '__main__':
    p1 = Practice()
    p1.run()
